﻿using Newtonsoft.Json;
using PMIS.Common;
using PMIS.Controls;
using PMIS.Dialogs;
using PMIS.HttpRequest;
using PMIS.Shared.DTO;
using PMIS.Shared.Models;
using PMIS.Views.DataDict;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;

namespace PMIS.ViewModels.DataDict
{
    public class DataDictViewModel : ViewModelBase
    {
        private readonly IDialogService _dialogService;
        private readonly HttpService _httpService;

        public DataDictViewModel(IDialogService dialogService, HttpService httpService)
        {
            _dialogService = dialogService;
            _httpService = httpService;
            DataDictList = new ObservableCollection<DataDictDto>();
            LoadDataDictList();
        }

        private async void LoadDataDictList()
        {
            var parameters = new Dictionary<string, object>();
            parameters["dictType"] = this.DictType;
            var response = await _httpService.GetAsync(Api.DATADICT_GET_LIST, parameters);
            if (response?.code == 1)
            {
                var data = JsonConvert.DeserializeObject<PagingModel<DataDictDto>>(JsonConvert.SerializeObject(response.data));
                if (data.Items != null)
                {
                    DataDictList = new ObservableCollection<DataDictDto>(data.Items);
                }
            }
        }

        private ObservableCollection<DataDictDto>? _dataDictList;
        public ObservableCollection<DataDictDto>? DataDictList
        {
            get
            {
                return _dataDictList;
            }
            set
            {
                SetProperty(ref _dataDictList, value);
            }
        }

        private string _dictType;

        public string DictType
        {
            get { return _dictType; }
            set { SetProperty(ref _dictType, value); }
        }

        private void OnAdd()
        {
            _dialogService.ShowDialog(nameof(AddEditDataDict), null, AddEditCallBack);
        }

        private async void OnDelete()
        {
            var count = DataDictList.Count(p => p.IsChecked == true);
            if (count == 0)
            {
                ToastManager.ShowToast("请选择要删除的数据");
                return;
            }
            var dialogResult = System.Windows.MessageBox.Show("是否删除选择的数据", "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning);
            if (dialogResult == MessageBoxResult.Yes)
            {
                List<string?> ids = DataDictList.Where(p => p.IsChecked == true).Select(p => p.Id).ToList();
                await DeleteDictList(ids);
                ids.ForEach((id) =>
                {
                    var r = DataDictList.First(p => p.Id == id);
                    DataDictList.Remove(r);
                });
            }
        }

        private async Task<bool> DeleteDictList(List<string?> ids)
        {
            var response = await _httpService.PostAsync(Api.DATADICT_DELETE, ids);
            if (response?.code == 1)
            {
                return true;
            }
            return false;
        }

        private void OnEdit(object obj)
        {
            if (obj is RoleDto role)
            {
                DialogParameters para = new DialogParameters();
                para.Add("data", role.DeepClone());
                _dialogService.ShowDialog(nameof(AddEditDataDict), para, AddEditCallBack);
            }
        }

        private void OnPrint()
        {

        }

        private void OnRefresh()
        {

        }

        private void OnSearch()
        {
            this.LoadDataDictList();
        }

        private ICommand _searchCommand;
        public ICommand SearchCommand => _searchCommand ??= new DelegateCommand(OnSearch);

        private ICommand _addCommand;
        public ICommand AddCommand => _addCommand ??= new DelegateCommand(OnAdd);

        private ICommand _editCommand;
        public ICommand EditCommand => _editCommand ??= new DelegateCommand<object>(obj => OnEdit(obj));

        private ICommand _deleteCommand;
        public ICommand DeleteCommand => _deleteCommand ??= new DelegateCommand(OnDelete);

        private ICommand _printCommand;
        public ICommand PrintCommand => _printCommand ??= new DelegateCommand(OnPrint);

        private ICommand _refreshCommand;
        public ICommand RefreshCommand => _refreshCommand ??= new DelegateCommand(OnRefresh);

        private void AddEditCallBack(IDialogResult dialogResult)
        {
            if (dialogResult != null && dialogResult.Result == ButtonResult.OK)
            {
                LoadDataDictList();
            }
        }
    }
}
